/*
 * Copyright (C) 2009 IMOBISOFT Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package imobisoft.android.hialarm.base;

import android.content.Context;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.util.Log;

import imobisoft.android.hialarm.base.BTDevice.OnChangeListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SettingsHelper {
	private static Context mContext = null;
	private static AudioManager mAudioManager = null;
	private static LocationManager mLocationManager = null;
	private static BTDevice mBTDevice = null;
	
	public SettingsHelper(Context context) {
		mContext = context;
		mAudioManager = (AudioManager) context
		.getSystemService(Context.AUDIO_SERVICE);
		mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		mBTDevice = new BTDevice(mContext);
		
		mBTDevice.setOnChangeListener(new OnChangeListener(){

			@Override
			public void onChange(BTDevice d) {
				Log.d(AlarmConst.TAG,"bt device state changed");
			}
			
		});
	}
	
	/**
	 * change wifi
	 */
	public static void changeWifi(boolean enabled) {
		//TODO:Should check permissions
		// android.permission.ACCESS_WIFI_STATE         
		// This requires two permissions:         
		// and android.permission.CHANGE_WIFI_STATE 
		WifiManager manager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		if(null != manager){
			manager.setWifiEnabled(enabled);
		}
	}
	
	/**
	 * micro phone
	 */
	public static void changeMicroPhone(boolean enabled){
		if (mAudioManager != null) {
			mAudioManager.setMicrophoneMute(enabled);
		}
	}
	
	/**
	 * speaker
	 */
	public static void changeSpeaker(boolean enabled){
		if (mAudioManager != null) {
			mAudioManager.setSpeakerphoneOn(enabled);

			if (enabled)
				mAudioManager.setRouting(AudioManager.MODE_IN_CALL,
						AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
			else
				mAudioManager
						.setRouting(AudioManager.MODE_IN_CALL,
								AudioManager.ROUTE_EARPIECE,
								AudioManager.ROUTE_ALL);
		}
	}
	
	private static boolean getGpsStatus()
	{
		String allowedLocationProviders =
			Settings.System.getString(mContext.getContentResolver(),
			Settings.System.LOCATION_PROVIDERS_ALLOWED);
	 
		if (allowedLocationProviders == null) {
			allowedLocationProviders = "";
		}
	 
		return allowedLocationProviders.contains(LocationManager.GPS_PROVIDER);
	}	
	
	/**
	 * gps
	 */
	public static void changeGPS(boolean enabled){
		/**
		 * if equal with current status ,remain unchanged
		 */
		boolean curGpsStatus = getGpsStatus();
		if(curGpsStatus == enabled){
			return;
		}
		
		String allowedLocationProviders =
			Settings.System.getString(mContext.getContentResolver(),
			Settings.System.LOCATION_PROVIDERS_ALLOWED);
		
		if (allowedLocationProviders == null) {
			allowedLocationProviders = "";
		}   	
	 
		boolean networkProviderStatus =
			allowedLocationProviders.contains(LocationManager.NETWORK_PROVIDER);
		
		if (networkProviderStatus == true) {
			allowedLocationProviders += LocationManager.NETWORK_PROVIDER;
		}
		if (enabled == true) {
			allowedLocationProviders += "," + LocationManager.GPS_PROVIDER;
		}	
	 
		Settings.System.putString(mContext.getContentResolver(),
			Settings.System.LOCATION_PROVIDERS_ALLOWED, allowedLocationProviders);
		try{
			Method m =
				mLocationManager.getClass().getMethod("updateProviders", new Class[] {});
			m.setAccessible(true);
			m.invoke(mLocationManager, new Object[]{});
		}
		catch(NoSuchMethodException e){
			Log.e(AlarmConst.TAG,"change gps NoSuchMethodException");
		}
		catch(SecurityException e){
			Log.e(AlarmConst.TAG,"change gps SecurityException");
		}
		catch(InvocationTargetException e){
			Log.e(AlarmConst.TAG,"change gps InvocationTargetException");
		}
		catch(IllegalAccessException e){
			Log.e(AlarmConst.TAG,"change gps IllegalAccessException");
		}
		return;
	}
	
	/**
	 * Enable/disable bluetooth
	 */
	public static void changeBluetooth(boolean enabled){
		 if(mBTDevice.isEnabled() == enabled){
			 return;
		 }
		 
		 mBTDevice.toggle();
	}
}

